package thirdly;

import javax.sound.sampled.*;
import java.io.File;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

public class TimerTest {
    public static int time = 5;
    public static void main(String[] args) {
        Timer t = new Timer();
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                while (time>0){
                    time--;
                    try {
                        Thread.sleep(1000);
                        int hh=0;
                        int mm = 0;
                        int ss = time % 60;
                        System.out.println("距离香港回归还剩" + hh + "小时" + mm + "分钟" + ss + "秒");
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                }
                try {
                    // 获取音频输入流
                    AudioInputStream audioInputStream = AudioSystem
                            .getAudioInputStream(new File("F:\\作业区\\java实训\\thirdly\\src\\main\\java\\com\\example\\demo\\music\\罗大佑 - 东方之珠 (KTV版伴奏).wav"));
                    // 获取音频编码对象
                    AudioFormat audioFormat = audioInputStream.getFormat();

                    // 设置数据输入
                    DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class,
                            audioFormat, AudioSystem.NOT_SPECIFIED);
                    SourceDataLine sourceDataLine = (SourceDataLine) AudioSystem
                            .getLine(dataLineInfo);
                    sourceDataLine.open(audioFormat);
                    sourceDataLine.start();

                    /*
                     * 从输入流中读取数据发送到混音器
                     */
                    int count;
                    byte tempBuffer[] = new byte[1024];
                    while ((count = audioInputStream.read(tempBuffer, 0, tempBuffer.length)) != -1) {
                        if (count > 0) {
                            sourceDataLine.write(tempBuffer, 0, count);
                        }
                    }

                    // 清空数据缓冲,并关闭输入
                    sourceDataLine.drain();
                    sourceDataLine.close();
                } catch (Exception e) {
                    
                    e.printStackTrace();
                }
            }
        };
        Calendar c = Calendar.getInstance();
        c.set(1997,07,01,00,00,00);
        System.out.println(c.getTime());
        t.schedule(task, c.getTime());
    }
}